package com.bt.dmdb.dao.impl;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bt.dmdb.dao.CustomerDetailsDao;
import com.bt.dmdb.model.BusinessNature;
import com.bt.dmdb.model.BusinessNatureDto;
import com.bt.dmdb.model.Contact;
import com.bt.dmdb.model.ContactDto;
import com.bt.dmdb.model.CustomerDetails;
import com.bt.dmdb.model.EagPassiveIncome;
import com.bt.dmdb.model.ExchangeMarketDto;
import com.bt.dmdb.model.Locations;
import com.bt.dmdb.model.LocationsDto;
import com.bt.dmdb.model.Market;
import com.bt.dmdb.model.MemberStatus;
import com.bt.dmdb.model.MemberStatusDto;
import com.bt.dmdb.model.PassiveIncomeDto;
import com.bt.dmdb.model.Regulators;
import com.bt.dmdb.model.RegulatorsDto;
import com.bt.dmdb.model.RestrictedDistributor;
import com.bt.dmdb.model.RestrictedDistributorDto;
import com.bt.dmdb.model.SourceIncome;
import com.bt.dmdb.model.SourceIncomeDto;
import com.bt.dmdb.model.SourceIncomeModel;
import com.bt.dmdb.model.StocksDistribution;
import com.bt.dmdb.model.StocksDistributionDto;

@Repository("customerDetailsDao")
public class CustomerDetailsDaoImpl extends GenericDaoImpl<CustomerDetails, Long>  implements CustomerDetailsDao  {

	public CustomerDetailsDaoImpl() {
		super(CustomerDetails.class);
	}

	//1st change - start
	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessNature> getAllBusinessNature(Long id) {
		 return (List<BusinessNature>) cs().createCriteria(BusinessNature.class).add(Restrictions.eq("customerid.id", id)).list();
	}


	@Override
	public void deleteBusinessNatureById(Long id) {
		BusinessNature businessNature = (BusinessNature) cs().createCriteria(BusinessNature.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(businessNature != null)
		cs().delete(businessNature);		
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessNature> saveAllBusinessNature(BusinessNatureDto businessNatureDto) {
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(businessNatureDto.getCustId()))).uniqueResult();
		for(BusinessNature businessNature:businessNatureDto.getBusinessNatures()){
			if(businessNature.getId()!=null){
				BusinessNature businessNatureLocal = (BusinessNature) cs().createCriteria(BusinessNature.class).add(Restrictions.eq("id", businessNature.getId())).uniqueResult();
				businessNatureLocal.setBusinessActivity(businessNature.getBusinessActivity());
				businessNatureLocal.setEntityName(businessNature.getEntityName());
				businessNatureLocal.setIndustryClass(businessNature.getIndustryClass());
				businessNatureLocal.setCustomerid(customerDetails);
		
				cs().update(businessNatureLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				businessNature.setCustomerid(customerDetails);
				cs().save(businessNature);
			}
		}
		return (List<BusinessNature>) cs().createCriteria(BusinessNature.class).add(Restrictions.eq("customerid.id", new Long(businessNatureDto.getCustId()))).list();
		
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberStatus> getAllEAGMemberStatus(Long id) {
		return (List<MemberStatus>) cs().createCriteria(MemberStatus.class).add(Restrictions.eq("customerid.id", id)).list();
	}

	@Override
	public void deleteEAGMemberStatusById(Long id) {
		MemberStatus memberStatus = (MemberStatus) cs().createCriteria(MemberStatus.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(memberStatus != null)
		cs().delete(memberStatus);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberStatus> saveAllEAGMemberStatus(
			MemberStatusDto memberStatusDto) {
		
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(memberStatusDto.getCustId()))).uniqueResult();
		for(MemberStatus memberStatus:memberStatusDto.getMemberStatus()){
			if(memberStatus.getId()!=null){
				MemberStatus memberStatusLocal = (MemberStatus) cs().createCriteria(MemberStatus.class).add(Restrictions.eq("id", memberStatus.getId())).uniqueResult();
				memberStatusLocal.setMemberName(memberStatus.getMemberName());
				memberStatusLocal.setStatus(memberStatus.getStatus());
				memberStatusLocal.setCustomerid(customerDetails);
		
				cs().update(memberStatusLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				memberStatus.setCustomerid(customerDetails);
				cs().save(memberStatus);
			}
		}
		return (List<MemberStatus>) cs().createCriteria(MemberStatus.class).add(Restrictions.eq("customerid.id", new Long(memberStatusDto.getCustId()))).list();
		
		
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Market> getAllExchangeMarket(Long id) {
		return (List<Market>) cs().createCriteria(Market.class).add(Restrictions.eq("customerid.id", id)).list();
	} 
	
	@Override
	public void deleteExchangeMarketById(Long id) {
		Market exchangeMarket = (Market) cs().createCriteria(Market.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(exchangeMarket != null)
		cs().delete(exchangeMarket);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Market> saveAllExchangeMarket(ExchangeMarketDto exchangeMarketDto) {
		
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(exchangeMarketDto.getCustId()))).uniqueResult();
		for(Market exchangeMarket:exchangeMarketDto.getExchangeMarket()){
			if(exchangeMarket.getId()!=null){
				Market exchangeMarketLocal = (Market) cs().createCriteria(Market.class).add(Restrictions.eq("id", exchangeMarket.getId())).uniqueResult();
				exchangeMarketLocal.setMarketName(exchangeMarket.getMarketName());
				exchangeMarketLocal.setTicket(exchangeMarket.getTicket());
				exchangeMarketLocal.setCustomerid(customerDetails);
		
				cs().update(exchangeMarketLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				exchangeMarket.setCustomerid(customerDetails);
				cs().save(exchangeMarket);
			}
		}
		return (List<Market>) cs().createCriteria(Market.class).add(Restrictions.eq("customerid.id", new Long(exchangeMarketDto.getCustId()))).list();
		
		
	}
	//1st change - end

	@SuppressWarnings("unchecked")
	@Override
	public List<EagPassiveIncome> getAllEAGPassiveIncome(Long id) {
		return (List<EagPassiveIncome>) cs().createCriteria(EagPassiveIncome.class).add(Restrictions.eq("customerid.id", id)).list();
	}

	@Override
	public void deleteEAGPassiveIncomeById(Long id) {
		EagPassiveIncome passiveIncome = (EagPassiveIncome) cs().createCriteria(EagPassiveIncome.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(passiveIncome != null)
		cs().delete(passiveIncome);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EagPassiveIncome> saveAllEAGPassiveIncome(PassiveIncomeDto passiveIncomeDto) {
		
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(passiveIncomeDto.getCustId()))).uniqueResult();
		for(EagPassiveIncome passiveIncome:passiveIncomeDto.getPassiveIncome()){
			if(passiveIncome.getId()!=null){
				EagPassiveIncome passiveIncomeLocal = (EagPassiveIncome) cs().createCriteria(EagPassiveIncome.class).add(Restrictions.eq("id", passiveIncome.getId())).uniqueResult();
				passiveIncomeLocal.setSourceName(passiveIncome.getSourceName());
				passiveIncomeLocal.setPercentage(passiveIncome.getPercentage());
				passiveIncomeLocal.setCustomerid(customerDetails);
		
				cs().update(passiveIncomeLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				passiveIncome.setCustomerid(customerDetails);
				cs().save(passiveIncome);
			}
		}
		return (List<EagPassiveIncome>) cs().createCriteria(EagPassiveIncome.class).add(Restrictions.eq("customerid.id", new Long(passiveIncomeDto.getCustId()))).list();
		
		
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<Locations> getAllLocations(Long id) {
		return (List<Locations>) cs().createCriteria(Locations.class).add(Restrictions.eq("customerid.id", id)).list();
	}

	@Override
	public void deleteLocationsById(Long id) {
		Locations locations = (Locations) cs().createCriteria(Locations.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(locations != null)
		cs().delete(locations);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Locations> saveAllLocations(LocationsDto locationsDto) {
		
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(locationsDto.getCustId()))).uniqueResult();
		for(Locations locations:locationsDto.getLocations()){
			if(locations.getId()!=null){
				Locations locationsLocal = (Locations) cs().createCriteria(Locations.class).add(Restrictions.eq("id", locations.getId())).uniqueResult();
				locationsLocal.setCity(locations.getCity());
				locationsLocal.setCountry(locations.getCountry());
				locationsLocal.setCustomerid(customerDetails);
		
				cs().update(locationsLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				locations.setCustomerid(customerDetails);
				cs().save(locations);
			}
		}
		return (List<Locations>) cs().createCriteria(Locations.class).add(Restrictions.eq("customerid.id", new Long(locationsDto.getCustId()))).list();
		
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Regulators> getAllRegulators(Long id) {
		return (List<Regulators>) cs().createCriteria(Regulators.class).add(Restrictions.eq("customerid.id", id)).list();
	}

	@Override
	public void deleteRegulatorsById(Long id) {
		Regulators regulators = (Regulators) cs().createCriteria(Regulators.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(regulators != null)
		cs().delete(regulators);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Regulators> saveAllRegulators(RegulatorsDto regulatorsDto) {
		
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(regulatorsDto.getCustId()))).uniqueResult();
		for(Regulators regulators:regulatorsDto.getRegulators()){
			if(regulators.getId()!=null){
				Regulators regulatorsLocal = (Regulators) cs().createCriteria(Regulators.class).add(Restrictions.eq("id", regulators.getId())).uniqueResult();
				regulatorsLocal.setRegulatorName(regulators.getRegulatorName());
				regulatorsLocal.setCountry(regulators.getCountry());
				regulatorsLocal.setCustomerid(customerDetails);
		
				cs().update(regulatorsLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				regulators.setCustomerid(customerDetails);
				cs().save(regulators);
			}
		}
		return (List<Regulators>) cs().createCriteria(Regulators.class).add(Restrictions.eq("customerid.id", new Long(regulatorsDto.getCustId()))).list();
		
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RestrictedDistributor> getAllRestrictedDistributor(Long id) {
		return (List<RestrictedDistributor>) cs().createCriteria(RestrictedDistributor.class).add(Restrictions.eq("customerid.id", id)).list();
	}

	@Override
	public void deleteRestrictedDistributorById(Long id) {
		RestrictedDistributor restrictedDistributor = (RestrictedDistributor) cs().createCriteria(RestrictedDistributor.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(restrictedDistributor != null)
		cs().delete(restrictedDistributor);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RestrictedDistributor> saveAllRestrictedDistributor(RestrictedDistributorDto restrictedDistributorDto) {
		
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(restrictedDistributorDto.getCustId()))).uniqueResult();
		for(RestrictedDistributor restrictedDistributor:restrictedDistributorDto.getRestrictedDistributor()){
			if(restrictedDistributor.getId()!=null){
				RestrictedDistributor restrictedDistributorLocal = (RestrictedDistributor) cs().createCriteria(RestrictedDistributor.class).add(Restrictions.eq("id", restrictedDistributor.getId())).uniqueResult();
				restrictedDistributorLocal.setName(restrictedDistributor.getName());
				restrictedDistributorLocal.setLocation(restrictedDistributor.getLocation());
				restrictedDistributorLocal.setTinNo(restrictedDistributor.getTinNo());
				restrictedDistributorLocal.setCustomerid(customerDetails);
		
				cs().update(restrictedDistributorLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				restrictedDistributor.setCustomerid(customerDetails);
				cs().save(restrictedDistributor);
			}
		}
		return (List<RestrictedDistributor>) cs().createCriteria(RestrictedDistributor.class).add(Restrictions.eq("customerid.id", new Long(restrictedDistributorDto.getCustId()))).list();
		
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SourceIncome> getAllSourceIncome(Long id) {
		return (List<SourceIncome>) cs().createCriteria(SourceIncome.class).add(Restrictions.eq("customerid.id", id)).list();
	}

	@Override
	public void deleteSourceIncomeById(Long id) {
		SourceIncome sourceIncome = (SourceIncome) cs().createCriteria(SourceIncome.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(sourceIncome != null)
		cs().delete(sourceIncome);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SourceIncome> saveAllSourceIncome(SourceIncomeDto sourceIncomeDto) {
		
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(sourceIncomeDto.getCustId()))).uniqueResult();
		for(SourceIncomeModel sourceIncomeModel:sourceIncomeDto.getSourceIncome()){
			if(sourceIncomeModel.getId()!=null){
				SourceIncome sourceIncomeLocal = (SourceIncome) cs().createCriteria(SourceIncome.class).add(Restrictions.eq("id", sourceIncomeModel.getId())).uniqueResult();
				sourceIncomeLocal.setSourceName(sourceIncomeModel.getSourceName());
				sourceIncomeLocal.setPercentage(new Double(sourceIncomeModel.getPercentage()));
				sourceIncomeLocal.setCustomerid(customerDetails);
		
				cs().update(sourceIncomeLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				SourceIncome sourceIncomeLocal = new SourceIncome();
				sourceIncomeLocal.setCustomerid(customerDetails);
				sourceIncomeLocal.setSourceName(sourceIncomeModel.getSourceName());
				sourceIncomeLocal.setPercentage(new Double(sourceIncomeModel.getPercentage()));
				cs().save(sourceIncomeLocal);
			}
		}
		return (List<SourceIncome>) cs().createCriteria(SourceIncome.class).add(Restrictions.eq("customerid.id", new Long(sourceIncomeDto.getCustId()))).list();
		
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<StocksDistribution> getAllStocksDistribution(Long id) {
		 return (List<StocksDistribution>) cs().createCriteria(StocksDistribution.class).add(Restrictions.eq("customerid.id", id)).list();
	}


	@Override
	public void deleteStocksDistributionById(Long id) {
		StocksDistribution stocksDistribution = (StocksDistribution) cs().createCriteria(StocksDistribution.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(stocksDistribution != null)
		cs().delete(stocksDistribution);		
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<StocksDistribution> saveAllStocksDistribution(StocksDistributionDto stocksDistributionDto) {
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(stocksDistributionDto.getCustId()))).uniqueResult();
		for(StocksDistribution stocksDistribution:stocksDistributionDto.getStocksDistribution()){
			if(stocksDistribution.getId()!=null){
				StocksDistribution stocksDistributionLocal = (StocksDistribution) cs().createCriteria(StocksDistribution.class).add(Restrictions.eq("id", stocksDistribution.getId())).uniqueResult();
				stocksDistributionLocal.setStockholderName(stocksDistribution.getStockholderName());
				stocksDistributionLocal.setTin(stocksDistribution.getTin());
				stocksDistributionLocal.setOwnedPercent(new Double(stocksDistribution.getOwnedPercent()));
				stocksDistributionLocal.setcAddress(stocksDistribution.getcAddress());
				stocksDistributionLocal.setValue(new Double(stocksDistribution.getValue()));
				stocksDistributionLocal.setCustomerid(customerDetails);
		
				cs().update(stocksDistributionLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				
				StocksDistribution stocksDistributionLocal = new StocksDistribution();
				stocksDistributionLocal.setStockholderName(stocksDistribution.getStockholderName());
				stocksDistributionLocal.setTin(stocksDistribution.getTin());
				stocksDistributionLocal.setOwnedPercent(new Double(stocksDistribution.getOwnedPercent()));
				stocksDistributionLocal.setcAddress(stocksDistribution.getcAddress());
				stocksDistributionLocal.setValue(new Double(stocksDistribution.getValue()));
				stocksDistributionLocal.setCustomerid(customerDetails);
				cs().save(stocksDistributionLocal);
			}
		}
		return (List<StocksDistribution>) cs().createCriteria(StocksDistribution.class).add(Restrictions.eq("customerid.id", new Long(stocksDistributionDto.getCustId()))).list();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Contact> getAllContacts(Long id) {
		return (List<Contact>) cs().createCriteria(Contact.class).add(Restrictions.eq("customerId.id", id)).list();
	}

	@Override
	public void deleteContactById(Long id) {
		Contact contact = (Contact) cs().createCriteria(Contact.class).add(Restrictions.eq("id", id)).uniqueResult();
		if(contact != null)
		cs().delete(contact);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Contact> saveAllContacts(ContactDto contactDto) {
		CustomerDetails customerDetails = (CustomerDetails) cs().createCriteria(CustomerDetails.class).add(Restrictions.eq("id", new Long(contactDto.getCustId()))).uniqueResult();
		for(Contact contact:contactDto.getContacts()){
			if(contact.getId()!=null){
				Contact contactLocal = (Contact) cs().createCriteria(Contact.class).add(Restrictions.eq("id", contact.getId())).uniqueResult();
				contactLocal.setContactType(contact.getContactType());
				contactLocal.setEmailId(contact.getEmailId());
				contactLocal.setFirstName(contact.getFirstName());
				contactLocal.setEmailId(contact.getEmailId());
				contactLocal.setPhoneNo(contactLocal.getPhoneNo());
				contactLocal.setMobileNo(contactLocal.getMobileNo());
				contactLocal.setCustomerId(customerDetails);
		
				cs().update(contactLocal);
			}
			else{
				//Role role = new Role(new Long(2),"maker");
				//l.setRole(role);
				//Role role = (Role) cs().createCriteria(Role.class).add(Restrictions.eq("id", new Long(2))).uniqueResult();
				//l.setRole(role);
				contact.setCustomerId(customerDetails);
				cs().save(contact);
			}
		}
		return (List<Contact>) cs().createCriteria(Contact.class).add(Restrictions.eq("customerId.id", new Long(contactDto.getCustId()))).list();
		
	}


}
